<?php
class Tpl extends ClassBase{
	
	private $template = 'default';
	private $scripts  = array();
	private $title 		= 'Title..';
	
	function __construct(){
		//Default Scripts
		$this->addScript('jquery');
		$this->addScript('modernizr', false);
		$this->addScript('jqueryUI', true);
		$this->addScript('bootstrap', true);
		$this->addScript('default', true);
		//		$this->addScript('normalize', true);
		$this->addScript('wysiwyg', true);//TODO move into a place that wont get repeated calls
		$this->addScript('jquery-form', true);
	}
	
	
	public function drawTop(){
		$defaultFR = 	$this->getTemplateFindReplace();
		$find 		 = $defaultFR[0];
		$replace 	 = $defaultFR[1];
		
		$find[] = '<!scripts:top>'; 	 $replace[] = $this->getScripts('top');
		$find[] = '<!navigation:primary>'; $replace[] = capture(F::get()->Nav(), 'draw');
		$find[] = '<!base>'; $replace[] = '<base href="'.BASE.'" />';
		$find[] = '<!title>'; $replace[] = $this->title;
		
		$tpl =  file_get_contents(ROOT.'template/'.$this->template.'/top.html');
		return str_replace($find, $replace, $tpl);
	}
	
	public function drawBottom(){
		$defaultFR = 	$this->getTemplateFindReplace();
		$find 		 = $defaultFR[0];
		$replace 	 = $defaultFR[1];
		
		$find[] 	= '<!scripts:bot>'; $replace[] = $this->getScripts('bot');
		
		$tpl =  file_get_contents(ROOT.'template/'.$this->template.'/bottom.html');
		return str_replace($find, $replace, $tpl);
	}
	
	private function getTemplateFindReplace(){
		$find 	= $replace = array();
		$find[] = '<!template:messages>'; $replace[] = $this->drawMessages();
		return array($find, $replace);
	}
	
	public function drawLogin(){
		if( F::get()->Auth()->isLoggedIn() ){
			$html = '
			<ul class="nav">
			<li class="dropdown">
			<a href="#"
			class="dropdown-toggle"
			data-toggle="dropdown">
			Account
			<b class="caret"></b>
			</a>
			<ul class="dropdown-menu">
			<li><a href="logout">Logout</a></li>
      </ul>
      </li>
      </ul>
			';
		}else{
			//TODO update this to be more secure
			$html = '
			<form action="'.BASE.'home" method="POST" class="form-inline pull-right" style="margin: 0 0 5px 10px" >
			<input type="text"  placeholder="Username" name="login[username]" class="input-small" style="margin: 5px 0 0 0; height: 16px;"/>
			<input type="password" placeholder="Password" name="login[password]" class="input-small" style="margin: 5px 0 0 0;height: 16px;" />
			<button type="submit" class="btn btn-primary"/>Login</button>
			<input type="hidden" name="action" value="Auth" />
			<input type="hidden" name="actionSwitch" value="login" />
			</form>
			';
		}
		return $html;
	}
	
	//TODO dont add html only the pathkey
	public function addScript($pathKey, $async = false){
		if( $path = F::get()->Paths()->getPaths($pathKey) ){
			//TODO 3rdparty url to be in a defined varible etc
			foreach($path as $url){
				$pathinfo = pathinfo($url);
				
				switch( $pathinfo['extension'] ){
					case 'css':{
						$this->scripts['top'][] = '<link href="system/3rdparty/scripts/'.$url.'" type="text/css" rel="stylesheet" />';
						break;
					}
					case 'js':{
						$this->scripts['bot'][] = '<script src="system/3rdparty/scripts/'.$url.'" '.( $async ? 'async="async"' : '').' type="text/javascript" ></script>';
						break;
					}
				}
			}
		}
	}
	public function addMessage($title, $message, $type = 'success'){
		$_SESSION['template']['message'][$type][] = array('title'=>$title, 'msg'=>$message);
	}
	public function drawMessages(){
		$html = '';
		if( $_SESSION['template']['message'] ){
			foreach($_SESSION['template']['message'] as $type=>$messages){
				foreach($messages as $val){
					$html .= $this->drawMessage($val['title'], $val['msg'], $type);
				}
			}
		}
		$_SESSION['template']['message'] = array();
		return $html;
	}
	public function drawMessage($title, $message, $type){
		$html = '
		<div class="alert alert-'.$type.'"><a class="close" data-dismiss="alert">&times;</a>'.
		'<h4 class="alert-heading">'.$title.'</h4>'.$message.'</div>';
		return $html;
		
	}
	
	/*==== Useful =====*/
	public function refresh(){
		header('Location: '.BASE);
		exit;
	}
	public function redirect($url, $internal = true){
		if($internal){
			header('Location: '.BASE.$url);
		}else{
			header('Location: '.$url);
		}
		exit;
	}
	
	/* Setters & Getters */
	//** GET
	private function getScripts($type){
		if( $this->scripts[$type] ){
			return implode(_LF,$this->scripts[$type]);
		}
	}
	//** SET
	public function setTitle($title){
		$this->title = $title;
	}
	
	public function post($switch){
		
	}
	
}

?>	
